//
//  FMFindHostViewController.m
//  PsychologyFM
//
//  Created by lanou on 16/1/16.
//  Copyright © 2016年 ZhangLuDu. All rights reserved.
//

#import "FMFindHostViewController.h"
#import "FMDiantaiModel.h"
#import "PKRequestManager.h"
#import "SDCycleScrollView.h"
#import "FMDiantaiTableViewCell.h"
#import "FMAntorDescViewController.h"
#import "FMHotHostTableViewCell.h"

@interface FMFindHostViewController ()<SDCycleScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *findHostTableView;

//  保存推荐主播的model数组
@property (nonatomic,strong) NSMutableArray *recommendHostDataArray;
//  保存新晋主播的model数组
@property (nonatomic,strong) NSMutableArray *NewHostDataArray;
//  保存热门主播的model数组
@property (nonatomic,strong) NSMutableArray *hotHostDataArray;

//  保存表头轮播图的网址数组
@property (nonatomic,strong) NSMutableArray *imgsUrlStrArray;

@end

@implementation FMFindHostViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    _rootVC.tabbar.hidden = YES;
    

}

- (void)viewWillDisappear:(BOOL)animated
{
    _rootVC.tabbar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = FMWhite;
    // Do any additional setup after loading the view.
    //添加视图
    [self addView];
    //请求数据
    [self requestData];
}
#pragma mark --  添加视图
- (void)addView
{
    _findHostTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:(UITableViewStyleGrouped)];
    //  设置代理和数据源
    _findHostTableView.delegate = self;
    _findHostTableView.dataSource = self;
    
    //  注册cell
    [_findHostTableView registerClass:[FMDiantaiTableViewCell class] forCellReuseIdentifier:@"FMDiantaiTableViewCell"];
    [_findHostTableView registerClass:[FMHotHostTableViewCell class] forCellReuseIdentifier:@"FMHotHostTableViewCell"];
    
    [_findHostTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"mycell"];
    [self.view addSubview:_findHostTableView];
    
    //  添加总表头的view
    //  添加总表头
    _findHostTableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,kScreenWidth / 2)];
    //_findHostTableView.tableHeaderView.backgroundColor = FMCyan;

}
#pragma mark --  请求数据
- (void)requestData
{
    //  给保存model的数组分配空间
    _recommendHostDataArray = [NSMutableArray array];
    _NewHostDataArray = [NSMutableArray array];
    _hotHostDataArray = [NSMutableArray array];
    //  为轮播图图片网址数组分配空间
    _imgsUrlStrArray = [NSMutableArray array];
    //  请求数据
    [PKRequestManager requestWithUrlString:_urlStr parDic:nil method:GET finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        //  取出解析出来的对应数组
        NSArray *recommendArray = dic[@"data"][@"tuijian"];
        NSArray *newArray = dic[@"data"][@"newlist"];
        NSArray *hotArray = dic[@"data"][@"hotlist"];
        
        //  遍历对应的数组,以model形势存入对应的数据源数组中
        for (NSDictionary *smDic in recommendArray) {
            FMDiantaiModel *model = [[FMDiantaiModel alloc]init];
            [model setValuesForKeysWithDictionary:smDic];
            [_recommendHostDataArray addObject:model];
            
            NSString *str = model.cover;
            [_imgsUrlStrArray addObject:str];
        }
        for (NSDictionary *smDic in newArray) {
            FMDiantaiModel *model = [[FMDiantaiModel alloc]init];
            [model setValuesForKeysWithDictionary:smDic];
            [_NewHostDataArray addObject:model];
        }
        for (NSDictionary *smDic in hotArray) {
            FMDiantaiModel *model = [[FMDiantaiModel alloc]init];
            [model setValuesForKeysWithDictionary:smDic];
            [_hotHostDataArray addObject:model];
        }
        //  添加轮播图
        SDCycleScrollView *topScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth / 2) imageURLStringsGroup:_imgsUrlStrArray];
        _findHostTableView.tableHeaderView.height = 200;
        topScrollView.backgroundColor = FMYellow;
        [_findHostTableView.tableHeaderView addSubview:topScrollView];
        //  设置轮播图的代理
        topScrollView.delegate = self;

        
        
        //  刷新界面
        [_findHostTableView reloadData];
    
        
    } error:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

#pragma mark -- findHostTableView的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    if (section == 1) {
        return _hotHostDataArray.count;
    }
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        FMDiantaiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FMDiantaiTableViewCell" forIndexPath:indexPath];
        cell.contentView.backgroundColor = FMRed;
        if (_NewHostDataArray.count == 0) {
            return cell;
        }else
        {
        cell.modelArray = _NewHostDataArray;
            cell.homeVC = self;
        return cell;
        }
    }
    if (indexPath.section == 1) {
        FMHotHostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FMHotHostTableViewCell" forIndexPath:indexPath];
        if (_hotHostDataArray.count == 0) {
            return cell;
        }else
        {
        FMDiantaiModel *model = _hotHostDataArray[indexPath.row];
        cell.model = model;
        return cell;
        }
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell" forIndexPath:indexPath];
    return cell;
    
}
//  返回分区的表头表尾
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *headview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 40)];
        leftView.backgroundColor = FMGreen;
        [headview addSubview:leftView];
        
        UILabel *headLabel = [[UILabel alloc]initWithFrame:CGRectMake(leftView.right + 10, 5, 200, 30)];
        headLabel.text = @"新晋主播";
        headLabel.textAlignment = NSTextAlignmentLeft;
        [headview addSubview:headLabel];
        
        return headview;
    }else
    {
        UIView *headview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 40)];
        leftView.backgroundColor = FMRed;
        [headview addSubview:leftView];
        
        UILabel *headLabel = [[UILabel alloc]initWithFrame:CGRectMake(leftView.right + 10, 5, 200, 30)];
        headLabel.text = @"热门主播";
        headLabel.textAlignment = NSTextAlignmentLeft;
        [headview addSubview:headLabel];
        return headview;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
        UIButton *moreHostButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
        [moreHostButton setTitle:@"更多主播" forState:(UIControlStateNormal)];
        [moreHostButton setTitleColor:FMBlack forState:(UIControlStateNormal)];
        [footerView addSubview:moreHostButton];
        return footerView;
    }
    return nil;
}
#pragma mark --  分区表头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 40;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 140;
    }
    
    return 90;
    
}

//  轮播图点击方法

#pragma mark -- 轮播图的点击方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    FMDiantaiModel *model = _recommendHostDataArray[index];
    FMAntorDescViewController *hostVC = [[FMAntorDescViewController alloc]init];
    hostVC.idStr = model.id;
    [self.navigationController pushViewController:hostVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
